// 组合模式 - 统计数据组织
export class StatisticsComponent {
  constructor(name) {
    this.name = name
  }

  add() {
    throw new Error('抽象方法，需要子类实现')
  }

  remove() {
    throw new Error('抽象方法，需要子类实现')
  }

  getStatistics() {
    throw new Error('抽象方法，需要子类实现')
  }
}

// 统计组合类 - 课程统计（复合组件）
export class CourseStatistics extends StatisticsComponent {
  constructor(name, courseId) {
    super(name)
    this.courseId = courseId
    this.children = []
  }
  
  add(component) {
    this.children.push(component)
  }
  
  remove(component) {
    const index = this.children.findIndex(child => child === component)
    if (index !== -1) {
      this.children.splice(index, 1)
    }
  }
  
  getStatistics() {
    return {
      name: this.name,
      id: this.courseId,
      children: this.children.map(child => child.getStatistics())
    }
  }
}

// 统计叶子类 - 实验统计（叶子组件）
export class ExperimentStatistics extends StatisticsComponent {
  constructor(name, experimentId, missingStudents, totalStudents) {
    super(name)
    this.experimentId = experimentId
    this.missingStudents = missingStudents
    this.totalStudents = totalStudents
  }
  
  add() {
    throw new Error('叶子节点不能添加子节点')
  }
  
  remove() {
    throw new Error('叶子节点不能移除子节点')
  }
  
  getStatistics() {
    return {
      experimentName: this.name,
      experimentId: this.experimentId,
      missingCount: this.missingStudents.length,
      submitRate: (this.totalStudents - this.missingStudents.length) / this.totalStudents,
      missingStudents: this.missingStudents
    }
  }
}

// 统计叶子类 - 学生统计（叶子组件）
export class StudentStatistics extends StatisticsComponent {
  constructor(student, missingExperiments) {
    super(student.name)
    this.student = student
    this.missingExperiments = missingExperiments
  }
  
  add() {
    throw new Error('叶子节点不能添加子节点')
  }
  
  remove() {
    throw new Error('叶子节点不能移除子节点')
  }
  
  getStatistics() {
    return {
      ...this.student,
      missingCount: this.missingExperiments.length,
      missingExperiments: this.missingExperiments
    }
  }
}

// 统计处理类 - 分析和处理统计数据
export class StatisticsProcessor {
  constructor(courseStats, students) {
    this.courseStats = courseStats
    this.students = students
  }
  
  // 获取学生维度统计
  getStudentStatistics() {
    const experimentStats = this.courseStats.children.map(child => child.getStatistics())
    
    return this.students.map(student => {
      const missingExperiments = []
      
      experimentStats.forEach(exp => {
        if (exp.missingStudents.some(s => s.id === student.id)) {
          missingExperiments.push(exp.experimentName)
        }
      })
      
      return {
        ...student,
        missingCount: missingExperiments.length,
        missingExperiments
      }
    })
  }
  
  // 获取实验维度统计
  getExperimentStatistics() {
    return this.courseStats.children.map(child => child.getStatistics())
  }
  
  // 获取所有统计数据
  getAllStatistics() {
    return {
      course: this.courseStats.getStatistics(),
      studentStats: this.getStudentStatistics(),
      experimentStats: this.getExperimentStatistics()
    }
  }
} 